package com.itboye.eweilisten.me.zhanghu;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.app.MyApplication;
import com.itboye.eweilisten.base.BaseActivity;
import com.itboye.eweilisten.me.login.LoginActivity;
import com.itboye.eweilisten.presenter.UserPresenter;
import com.itboye.eweilisten.util.TimeCount;
import com.itboye.eweilisten.util.loadingutil.MAlert;
import com.itboye.eweilisten.util.widget.IsStringSPUtils;
import com.itboye.eweilisten.volley.ResultEntity;

import java.util.Observable;
import java.util.Observer;
import java.util.regex.Pattern;

/**
 * 更新邮箱
 */
public class UpdateEmailActivity extends BaseActivity implements Observer {
    private ImageView back;
    private TextView tvTitle;
    private EditText update_email_old;
    private EditText update_email_new;
    private EditText update_email_new02;
    private EditText update_email_yzm;
    private EditText new_update_email_yzm;
    private TextView update_email_code;
    private TextView new_update_email_code;
    private Button update_email_ok;
    private String OLDEMAIL;//旧邮箱地址
    private String NEWEMAIL;//新邮箱地址
    private String OKEMAIL;//确认新邮箱地址
    private String CODE;//验证码
    private String NEWCODE;//新验证码
    private TimeCount time1,time2;
    private UserPresenter presenter;
    public static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_email);
        presenter = new UserPresenter(this);
        tvTitle.setText(R.string.genghuan_email);

    }

    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.update_email_ok://确认修改邮箱
                CODE = update_email_yzm.getText().toString().trim();
                NEWCODE=new_update_email_yzm.getText().toString().trim();
                OLDEMAIL = update_email_old.getText().toString().trim();//旧邮箱地址
                NEWEMAIL = update_email_new.getText().toString().trim();//新邮箱地址
                OKEMAIL = update_email_new02.getText().toString().trim();
                if (OLDEMAIL.equals("") || NEWEMAIL.equals("") || OKEMAIL.equals("")) {
                    MAlert.alert("请输入邮箱地址");
                    return;
                }
                if (!(Pattern.matches(REGEX_EMAIL, OLDEMAIL) && Pattern.matches(REGEX_EMAIL, NEWEMAIL))) {
                    MAlert.alert("请输入正确的邮箱地址");
                    return;
                }
                if (!NEWEMAIL.equals(OKEMAIL)) {
                    MAlert.alert("两次邮箱输入不一致");
                    update_email_new02.setText("");
                    return;
                }
                if (OKEMAIL.equals(OLDEMAIL)) {
                    MAlert.alert("新邮箱不能与旧邮箱相同");
                    return;
                }
                if (CODE.equals("")) {
                    MAlert.alert("验证码不能为空");
                    return;
                }
                if(NEWCODE.equals("")){
                    MAlert.alert("验证码不能为空");
                }
                showProgressDialog("修改中...", true);
                Log.d("OLDEMAIL", OLDEMAIL + "\t" + NEWEMAIL + "\t" + CODE);
                presenter.onUpdateemil(OLDEMAIL, NEWEMAIL, CODE,NEWCODE, IsStringSPUtils.IsUid(),IsStringSPUtils.getSid());
                break;
            case R.id.update_email_code://获取原邮箱验证码
                OLDEMAIL = update_email_old.getText().toString().trim();//旧邮箱地址
                NEWEMAIL = update_email_new.getText().toString().trim();//新邮箱地址
                OKEMAIL = update_email_new02.getText().toString().trim();
                if (OLDEMAIL.equals("") || NEWEMAIL.equals("") || OKEMAIL.equals("")) {
                    MAlert.alert("请输入邮箱地址");
                    return;
                }
                if (!(Pattern.matches(REGEX_EMAIL, OLDEMAIL) && Pattern.matches(REGEX_EMAIL, NEWEMAIL))) {
                    MAlert.alert("请输入正确的邮箱地址");
                    return;
                }
                if (!NEWEMAIL.equals(OKEMAIL)) {
                    MAlert.alert("两次邮箱输入不一致");
                    update_email_new02.setText("");
                    return;
                }
                if (OKEMAIL.equals(OLDEMAIL)) {
                    MAlert.alert("新邮箱不能与旧邮箱相同");
                    return;
                }
                presenter.onSecurCode(OLDEMAIL, "6");
                time1 = new TimeCount(60000, 1000, update_email_code);// 构造CountDownTimer对象
                time1.start();
                break;
            case R.id.new_update_email_code://获取新邮箱验证码
                CODE = update_email_yzm.getText().toString().trim();
                OLDEMAIL = update_email_old.getText().toString().trim();//旧邮箱地址
                NEWEMAIL = update_email_new.getText().toString().trim();//新邮箱地址
                OKEMAIL = update_email_new02.getText().toString().trim();
                if (OLDEMAIL.equals("") || NEWEMAIL.equals("") || OKEMAIL.equals("")) {
                    MAlert.alert("请输入邮箱地址");
                    return;
                }
                if (!(Pattern.matches(REGEX_EMAIL, OLDEMAIL) && Pattern.matches(REGEX_EMAIL, NEWEMAIL))) {
                    MAlert.alert("请输入正确的邮箱地址");
                    return;
                }
                if (!NEWEMAIL.equals(OKEMAIL)) {
                    MAlert.alert("两次邮箱输入不一致");
                    update_email_new02.setText("");
                    return;
                }
                if (OKEMAIL.equals(OLDEMAIL)) {
                    MAlert.alert("新邮箱不能与旧邮箱相同");
                    return;
                }
                if("".equals(CODE)){
                    MAlert.alert("验证码不能为空");
                }
                presenter.onSecurCode(NEWEMAIL, "6");
                time2 = new TimeCount(60000, 1000, new_update_email_code);// 构造CountDownTimer对象
                time2.start();
                break;
        }
    }

    @Override
    public void update(Observable o, Object arg) {
        try {
            closeProgressDialog();
        } catch (Exception e) {

        }
        ResultEntity entity = handlerError(arg);
        if (entity != null) {
            if (entity.getCode().equals("1111")) {
                Intent intent = new Intent(MyApplication.instance, LoginActivity.class);
                startActivity(intent);
                return;
            }
            if (entity.getEventType() == presenter.send_code_success) {
                MAlert.alert("" + entity.getData());

            } else if (entity.getEventType() == presenter.send_code_fail) {
                MAlert.alert("" + entity.getData());
            } else if (entity.getEventType() == presenter.Update_Emeal_success) {
                MAlert.alert("" + entity.getData());
                finish();
            } else if (entity.getEventType() == presenter.Update_Emeal_fail) {
                MAlert.alert("" + entity.getData());
            }
        }
    }
}
